//
//  HomeProvinceController.m
//  KYProject
//
//  Created by Roman on 16/12/21.
//  Copyright © 2016年 Roman. All rights reserved.
//

#import "HomeProvinceController.h"
#import "ProvinceModel.h"
#import "HomeCityController.h"

@interface HomeProvinceController ()


@end

@implementation HomeProvinceController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    //取消分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.rowHeight = 50;
    
    [self loadData];
    
    self.navigationItem.title = @"选择省份";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:15]};
    
    [self setUpLeftButtonItem];
    
}

//左上角的箭头设置
- (void)setUpLeftButtonItem{
    
    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [leftBtn setImage:[UIImage imageNamed:@"jiantou"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(comeBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
}

- (void)comeBack{
    
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark
#pragma mark - 加载数据
- (void)loadData
{
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"province.plist" ofType:nil];
    
    NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
    
    _modeList = [NSArray yy_modelArrayWithClass:[ProvinceModel class] json:arr];
    
   // NSLog(@"%@",_modeList);
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _modeList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    ProvinceModel *model = _modeList[indexPath.row];
    
    cell.textLabel.text = model.name;
    
    //设置分割线
    UIImageView *threadView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"link_button_01"]];
    [cell.contentView addSubview:threadView];
    
    [threadView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(cell.contentView);
        make.height.mas_equalTo(3);
    }];
    
    return cell;
}

//点击相应的cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeCityController *city = [[HomeCityController alloc]init];
    
    ProvinceModel *model = _modeList[indexPath.row];
    
    city.CityList = model.cities;
    
    //点击跳转到城市列表
    [self.navigationController pushViewController:city animated:YES];
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
